import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.mortennobel.imagescaling.ResampleOp;

public class ImageCropDemo {

	public static void main(String[] args) throws IOException {
		scaleWallpaper();

	}

	private static void scaleWallpaper() throws IOException,
			FileNotFoundException {
		
		File file = new File("D:\\s3_uploader\\YY02\\05.png");

			int w = 960, h = 800;

			BufferedImage image1 = ImageIO.read(file);
			int src_width = image1.getWidth();
			int src_height = image1.getHeight();
			////裁剪，裁剪左右两边或上下两边，保留中间
			int sub_width = Math.min(src_width, Math.round(w * src_height / h));
			int sub_height = Math
					.min(src_height, Math.round(h * src_width / w));
			image1 = image1.getSubimage(
					Math.round((src_width - sub_width) / 2),
					Math.round((src_height - sub_height) / 2), sub_width,
					sub_height);
			
			//缩放
			ResampleOp resampleOp = new ResampleOp(w, h);
			BufferedImage image2 = resampleOp.filter(image1, null);
			FileOutputStream out = null;
			
			out = new FileOutputStream("D:\\s3_uploader\\YY02\\05_crop.png");
			ImageIO.write(image2, "png", out);
	}
}